﻿/*************************************************************************************
 * 文 件 名:   HK_VideoCtrl
 * 
 * 描    述:   视频播放功能
 * 
 * 版    本：  V1.0
 * 创 建 者：  谭潇杰、京产肠饭
 * 创建时间：  2021/9/15
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace HKTools
{
    public class HK_VideoCtrl : MonoBehaviour
    {
        RectTransform videoPanel_TF;
        Vector2 initSize;

        public VideoPlayer videoPlayer;

        // 进度滑动条
        Slider video_Slider;
        // 视频暂停提示
        Image pause_Img;
        // 视频关闭按钮
        Button close_Btn;

        //鼠标抬起或按下
        bool isMouseUp = true;
        bool isPause = false;

        // 全屏按钮
        Button fullScreen_Btn;

        [Header("全屏按钮两个图片：")]
        public Sprite fullScreen_Sprite;
        public Sprite notFullScreen_Sprite;

        [Header("全屏按钮提示文本：")]
        public Text fullScreen_Txt;
        bool isFullScreen = false;

        // 静音按钮
        Button mute_Btn;

        [Header("静音按钮两个图片:")]
        public Sprite mute_Sprite;
        public Sprite notMute_Sprite;

        [Header("静音按钮提示文本：")]
        public Text mute_Txt;
        bool isMute = false;

        // 视频时长文本标签
        Text videoTime_Txt;
        //视频时长
        int clipMinute, clipSecond, currentMinute, currentSecond;

        void Awake()
        {
            videoPanel_TF = transform.GetComponent<RectTransform>();
            video_Slider = transform.Find("Video_Slider").GetComponent<Slider>();
            pause_Img = transform.Find("Pause_Img").GetComponent<Image>();
            close_Btn = transform.Find("Close_Btn").GetComponent<Button>();
            fullScreen_Btn = transform.Find("FullScreen_Btn").GetComponent<Button>();
            mute_Btn = transform.Find("Mute_Btn").GetComponent<Button>();
            videoTime_Txt = transform.Find("VideoTime_Txt").GetComponent<Text>();

            videoPlayer.prepareCompleted += OnPrepareComplete;
        }

        void OnEnable()
        {
            initSize = videoPanel_TF.sizeDelta;

            pause_Img.gameObject.SetActive(false);

            close_Btn.onClick.AddListener(OnClickCloseBtn);

            video_Slider.onValueChanged.AddListener(OnSliderValueChange);

            fullScreen_Btn.onClick.AddListener(OnClickFullScreenBtn);
            mute_Btn.onClick.AddListener(OnClickMuteBtn);
            videoPlayer.targetTexture.Release();

            isFullScreen = false;
            isMute = false;
        }

        void Update()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            if (isMouseUp)
                video_Slider.value = videoPlayer.frame / (videoPlayer.frameCount * 1.0f);

            currentMinute = (int)(videoPlayer.time) / 60;
            currentSecond = (int)(videoPlayer.time - currentMinute * 60);
            videoTime_Txt.text = string.Format("{0:D2}:{1:D2} / {2:D2}:{3:D2}", currentMinute, currentSecond, clipMinute, clipSecond);
        }

        void OnPrepareComplete(VideoPlayer source)
        {
            source = videoPlayer;
            clipMinute = (int)(source.length) / 60;
            clipSecond = (int)(source.length - clipMinute * 60);
        }

        /// <summary>
        /// 通过 VideoClip 播放视频
        /// </summary>
        /// <param name="videoClip"></param>
        public void PlayAssignedVideo(VideoClip videoClip)
        {
            fullScreen_Txt.text = "全 屏";
            fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
            videoPanel_TF.sizeDelta = initSize;
            isFullScreen = false;

            videoPlayer.SetDirectAudioMute(0, false);
            mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
            mute_Txt.text = "静 音";
            isMute = false;

            video_Slider.value = 0;

            if (videoClip != null)
                videoPlayer.clip = videoClip;

            videoPlayer.Play();
        }

        /// <summary>
        /// 通过 URL 播放视频
        /// </summary>
        /// <param name="url"></param>
        public void PlayAssignedVideo(string url)
        {
            fullScreen_Txt.text = "全 屏";
            fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
            videoPanel_TF.sizeDelta = initSize;
            isFullScreen = false;

            videoPlayer.SetDirectAudioMute(0, false);
            mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
            mute_Txt.text = "静 音";
            isMute = false;

            video_Slider.value = 0;

            if (!string.IsNullOrEmpty(url))
                videoPlayer.url = url;

            videoPlayer.Play();
        }


        /// <summary>
        /// 点击关闭按钮
        /// </summary>
        void OnClickCloseBtn()
        {
            gameObject.SetActive(false);

            fullScreen_Txt.text = "全 屏";
            fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
            videoPanel_TF.sizeDelta = initSize;
            isFullScreen = false;

            videoPlayer.SetDirectAudioMute(0, false);
            mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
            mute_Txt.text = "静 音";
            isMute = false;
        }

        /// <summary>
        /// 改变播放进度条时
        /// </summary>
        /// <param name="value"></param>
        void OnSliderValueChange(float value)
        {
            if (isMouseUp)
                return;

            videoPlayer.frame = long.Parse((value * videoPlayer.frameCount).ToString("0."));
        }

        //全屏
        void OnClickFullScreenBtn()
        {
            if (isFullScreen)
            {
                videoPanel_TF.sizeDelta = initSize;
                fullScreen_Btn.GetComponent<Image>().sprite = fullScreen_Sprite;
                fullScreen_Txt.text = "全 屏";
            }
            else
            {
                videoPanel_TF.sizeDelta = new Vector2(Screen.width, Screen.height);
                fullScreen_Btn.GetComponent<Image>().sprite = notFullScreen_Sprite;
                fullScreen_Txt.text = "取消全屏";
            }
            isFullScreen = !isFullScreen;
        }

        //静音
        void OnClickMuteBtn()
        {
            if (isMute)
            {
                videoPlayer.SetDirectAudioMute(0, false);
                mute_Btn.GetComponent<Image>().sprite = notMute_Sprite;
                mute_Txt.text = "静 音";
            }
            else
            {
                videoPlayer.SetDirectAudioMute(0, true);
                mute_Btn.GetComponent<Image>().sprite = mute_Sprite;
                mute_Txt.text = "取消静音";
            }
            isMute = !isMute;
        }

        //鼠标按下
        public void OnValueSliderPointerDown()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            videoPlayer.Pause();
            videoPlayer.frame = long.Parse((video_Slider.value * videoPlayer.frameCount).ToString("0."));
            isMouseUp = false;
            isPause = false;
        }

        //鼠标抬起
        public void OnValueSliderPointerUp()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            videoPlayer.Play();
            isMouseUp = true;
            pause_Img.gameObject.SetActive(false);
        }

        //拖动开始
        public void OnValueSliderBeginDrag()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            isMouseUp = false;
        }

        //拖动结束
        public void OnValueSliderEndDrag()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            isMouseUp = true;
        }

        //设置视频暂停
        public void SetPauseImg()
        {
            if (videoPlayer.clip == null && videoPlayer.url == "")
                return;

            isPause = !isPause;
            pause_Img.gameObject.SetActive(isPause);
            if (!isPause)
                videoPlayer.Play();
            else
                videoPlayer.Pause();
        }
    }
}